package com.zx.platform.dao.impl;

import com.zx.platform.dao.OperatorInfoDao;
import com.zx.platform.dao.mapper.OperatorInfoMapper;
import com.zx.platform.dao.mapper.OperatorRolesMapper;
import com.zx.platform.dao.mapper.SequenceMapper;
import com.zx.platform.dao.model.*;
import com.zx.platform.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-1
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Component("OperatorInfoDao")
public class OperatorInfoImpl implements OperatorInfoDao {

	@Autowired
	private OperatorInfoMapper mapper;
	
    @Autowired
    private SequenceMapper sequencemapper;
    
    @Autowired
    private OperatorRolesMapper operatorRolesMapper;

    /**
     * 新增
     */
    public String add(OperatorInfo operatorInfo) throws DataAccessException {
    	/**
    	 *  add by wenjw 2015.5.13 
    	 *  操作员O+sequence(operator)
			商户 M+sequence（merchant）
			代理商 A+sequence（agent）
    	 */
    	operatorInfo.setOperatorId("O"+sequencemapper.selectByStrAndNextval("operator"));
    	operatorInfo.setCreateDate(new Date());
        int column = mapper.insert(operatorInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return operatorInfo.getOperatorId();
    }

    /**
     * 修改
     * @param operatorInfo
     * @return
     * @throws DataAccessException
     */
    public String update(OperatorInfo operatorInfo) throws DataAccessException {
    	operatorInfo.setUpdateDate(new Date());
        int column = mapper.updateByPrimaryKeySelective(operatorInfo);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return operatorInfo.getOperatorId();
    }

    /**
     * 删除
     */
    public void delete(OperatorInfo operatorInfo) throws DataAccessException {
        int column = mapper.deleteByPrimaryKey(operatorInfo.getOperatorId());
        if (column != 1) {
            throw new RuntimeException("");
        }
    }

    /**
     * 分页查找
     */
	public List<OperatorInfo> getPageByExample(int pageSize, int pageBegin,
			OperatorInfoExample example) throws DataAccessException {
		return mapper.getPageByModel(pageSize, pageBegin, example); 
	}
	/**
	 * 查找条数
	 * 
	 * @param example
	 * @return
	 * @throws DataAccessException
	 */
	public int countByExample(OperatorInfoExample example) throws DataAccessException {
		return mapper.countByExample(example);
	}

	/**
	 * 
	 */
	public List<OperatorInfo> getListByExample(OperatorInfoExample example)
			throws DataAccessException {
		return mapper.selectByExample(example); 
	}

	public OperatorInfo selectByPrimaryKey(String key) throws DataAccessException {
		return mapper.selectByPrimaryKey(key);
	}

	// 如果有事务,那么加入事务,没有的话新建一个(不写的情况下)
    @Transactional(propagation=Propagation.REQUIRED)
	public String setOperatorRoles(OperatorRolesTemp model)
			throws DataAccessException {
		String operatorId = model.getOperatorId();
		String roleIds = model.getRoleIds();
		// 先删除关联关系
		OperatorRolesExample operatorRolesExample = new OperatorRolesExample(); 
		com.zx.platform.dao.model.OperatorRolesExample.Criteria criteria = operatorRolesExample.createCriteria();
		criteria.andOperatorIdEqualTo(operatorId);
		operatorRolesMapper.deleteByExample(operatorRolesExample);
		// 再增加
		if (StrUtils.isNotEmpty(roleIds)) {
			String[] roleIdStrs = roleIds.split(";");
			for (int i = 0; i < roleIdStrs.length; i++) {
				String roleIdStr = roleIdStrs[i];
				if (StrUtils.isNotEmpty(roleIdStr)) {
					Integer roleId = 0;
					try {
						roleId = Integer.valueOf(roleIdStr);
						OperatorRoles record = new OperatorRoles();
						record.setRoleId(roleId);
						record.setOperatorId(operatorId);
						record.setCreateDate(new Date());
						operatorRolesMapper.insert(record);
					} catch (Exception e) {
						// 转换异常则不处理
					}
				}
			}
		}
		return operatorId;
	}

    /**
	 * 查找用户角色关联关系
	 */
	public List<OperatorRoles> getOperatorRoles(String operatorId)
			throws DataAccessException {
		OperatorRolesExample operatorRolesExample = new OperatorRolesExample();
		operatorRolesExample.createCriteria().andOperatorIdEqualTo(operatorId);
		return operatorRolesMapper.selectByExample(operatorRolesExample);
	}
}
